package org.wlldTest.lesson12;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.dromara.easyai.config.RZ;
import org.dromara.easyai.entity.ThreeChannelMatrix;
import org.dromara.easyai.function.ReLu;
import org.dromara.easyai.function.Tanh;
import org.dromara.easyai.matrixTools.Matrix;
import org.dromara.easyai.nerveCenter.ModelParameter;
import org.dromara.easyai.nerveCenter.NerveManager;
import org.dromara.easyai.nerveEntity.SensoryNerve;
import org.wlldTest.lesson8.Back;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Test {
    public static NerveManager nerveManager;

    public static void init() throws Exception {
        nerveManager = new NerveManager(20, 9, 2,
                1, new Tanh(), 0.01f, RZ.NOT_RZ, 0, 0, 0.9f, 5, false);
        nerveManager.initImageNet(1, 3, 40, 40, false, true, 0.01f
                , new ReLu(), 3, 0.01f, true);
        ModelParameter modelParameter = JSONObject.parseObject(Model.model, ModelParameter.class);
        nerveManager.insertConvModel(modelParameter);
    }

    public static void putPicture(ThreeChannelMatrix pic, boolean isStudy, Map<Integer, Float> E, Back back
            , long eventID) throws Exception {
        SensoryNerve input = nerveManager.getConvInput();
        input.postThreeChannelMatrix(eventID, pic, isStudy, E, back, false);
    }

    private static ThreeChannelMatrix getPicture() {
        ThreeChannelMatrix th1 = new ThreeChannelMatrix();
        Matrix r1 = new Matrix(40, 40);
        Matrix g1 = new Matrix(40, 40);
        Matrix b1 = new Matrix(40, 40);
        th1.setMatrixR(r1);
        th1.setMatrixG(g1);
        th1.setMatrixB(b1);
        th1.setX(40);
        th1.setY(40);
        return th1;
    }

    public static void main(String[] args) throws Exception {
        init();
        test();
        //study();
    }

    public static void study() throws Exception {
        Random random = new Random();
        Map<Integer, Float> E1 = new HashMap<>();
        Map<Integer, Float> E2 = new HashMap<>();
        for (int t = 0; t < 2000; t++) {
            System.out.println("================================" + t);
            E1.clear();
            E2.clear();
            E1.put(1, 0.5f);
            E1.put(2, 0.3f);
            E2.put(1, -0.3f);
            E2.put(2, -0.8f);
            ThreeChannelMatrix t1 = getPicture();
            ThreeChannelMatrix t2 = getPicture();
            float r1 = random.nextFloat() * 0.1f;
            float g1 = r1 + 0.2f;
            float b1 = r1 + 0.3f;
            float r2 = random.nextFloat() * 0.1f + 0.2f;
            float g2 = r2 + 0.5f;
            float b2 = r2 + 0.2f;
            for (int i = 0; i < 40; i++) {
                for (int j = 0; j < 40; j++) {
                    t1.getMatrixR().setNub(i, j, r1);
                    t1.getMatrixG().setNub(i, j, g1);
                    t1.getMatrixB().setNub(i, j, b1);
                    t2.getMatrixR().setNub(i, j, r2);
                    t2.getMatrixG().setNub(i, j, g2);
                    t2.getMatrixB().setNub(i, j, b2);
                }
            }
            putPicture(t1, true, E1, null, 1);
            putPicture(t2, true, E2, null, 2);
        }
        ModelParameter modelParameter = nerveManager.getConvModel();
        String model = JSON.toJSONString(modelParameter);
        System.out.println(model);
    }

    public static void test() throws Exception {
        Random random = new Random();
        for (int t = 0; t < 10; t++) {
            Back back = new Back();
            System.out.println("================================" + t);
            ThreeChannelMatrix t1 = getPicture();
            ThreeChannelMatrix t2 = getPicture();
            float r1 = random.nextFloat() * 0.1f;
            float g1 = r1 + 0.2f;
            float b1 = r1 + 0.3f;
            float r2 = random.nextFloat() * 0.1f + 0.2f;
            float g2 = r2 + 0.5f;
            float b2 = r2 + 0.2f;
            for (int i = 0; i < 40; i++) {
                for (int j = 0; j < 40; j++) {
                    t1.getMatrixR().setNub(i, j, r1);
                    t1.getMatrixG().setNub(i, j, g1);
                    t1.getMatrixB().setNub(i, j, b1);
                    t2.getMatrixR().setNub(i, j, r2);
                    t2.getMatrixG().setNub(i, j, g2);
                    t2.getMatrixB().setNub(i, j, b2);
                }
            }
            //putPicture(t1, false, null, back,1);
            putPicture(t2, false, null, back, 1);
            System.out.println("v1:" + back.getV1());
            System.out.println("v2:" + back.getV2());

        }
    }
}
